<?php 
ob_start();
session_start();
require('gest_login.php');
require('languages/it.php');
require('structure.php');
reserved_page();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
	var page = "profile";
</script>
<?php
get_header();
get_css();
get_js();

$user = get_user_data($_SESSION['id']);

?>
<script type="text/javascript" src="js/validate.js"></script>
</head>
<!-- recupero le mail dal db in php -->
<?php
include("database.php"); 	
mysql_select_db($database_database, $database);
$query = "SELECT mail FROM users";
$result = mysql_query($query, $database) or die(mysql_error());
$result_rows = mysql_num_rows($result);

//creo array php con indirizzi mail
$i=0;
while($row=mysql_fetch_array($result)){
$mails[$i] = $row['mail'];
$i++;
}

?>
<!-- salvo in un array Js le mail del DB -->
<script language="javascript"> 
var mails = new Array(); 
<? for($i=0; $i<$result_rows; $i++) { ?> 
mails[<?= $i ?>] = "<?= $mails[$i] ?>"; 
<? } ?> 

</script>
<body class="grunge">
<div id="wrapper">
	<div id="header_container">
		<div id="header">
			<h1 id="logo"> <a href="index.html">Xero HTML &amp; CSS Template</a> </h1>
			<!-- end logo -->
			
			<?php get_nav(); ?>
			
			<div class="clear"></div>
			<!-- clears floted logo & nav -->
			
			<div id="page_title">
				<ul class="breadcrumb_nav">
					<li><a href="../">Home</a></li>
					<li><a href="#">Profilo</a></li>
				</ul>
				<h1>Profilo</h1>
			</div>
			<!-- end page title -->
			
		</div>
		<!-- end header (940px width) --> 
	</div>
	<!-- end header container (100% width) -->
	<div id="body_content">
		<div id="content" class="">
			<div class="half">
				<h2>Dati Personali<em class="heading_description"></em></h2>
				<table> 
				<tbody> 
					<tr> 
						<th>Nome</th> 
						<td><?php echo $user['name'] ?></td> 
					</tr> 
					<tr> 
						<th>Cognome</th> 
						<td><?php echo $user['surname'] ?></td> 
					</tr>
					<tr> 
						<th>Mail</th> 
						<td><?php echo $user['mail'] ?></td> 
					</tr>
					<tr> 
						<th>Luogo</th> 
						<td><?php echo $user['place'] ?></td>  
					</tr> 
				</tbody> 
			</table> 
			<?php if(!$_SESSION['id_fb']) { ?>
			<a href="fb_connect.php"><img src="images/facebook_connect.png" alt="Connect With Facebook" id="fb_connect"/></a>
			<?php } ?>
			</div>
			<div class="half">
			<?php if(!$_SESSION['id_fb']) { ?>
			<h2>Modifica Dati Personali<em class="heading_description">Inserisci solo i dati che vuoi modificare, i campi vuoti lasceranno inalterati i dati corrispondenti.</em></h2>
			<form id="mod" method="post" name="mod">
				<ol>
				<li>
					<label for="name" class="auto_clear">Nome</label>
					<input type="text" name="name" id="name" class="text_field small" style="display:inline;" onblur="check_name()">
					<span id="no_name" class="error_notice" style="margin-left:10px; padding:6px; display:none; padding-left:45px;" >Inserire il nome</span>
				</li>
				<li>
					<label for="surname" class="auto_clear">Cognome</label>
					<input type="text" name="surname" id="surname" class="text_field small" style="display:inline;" onblur="check_surname()">
					<span id="no_surname" class="error_notice" style="margin-left:10px; padding:6px; padding-left:45px; display:none;" >Inserire il cognome</span>
				</li>
				<li>
					<label for="mail" class="auto_clear">Mail</label>
					<input type="text" name="mail" id="mail" class="text_field small" style="display:inline;" onblur="check_mail()">
					<span id="no_mail" class="error_notice" style="margin-left:10px; padding:6px; padding-left:45px; display:none;" >Inserire la mail</span>
					<span id="invalid_mail" class="error_notice" style="margin-left:10px; padding:6px; padding-left:45px; display:none;" >Mail non valida</span>
					<span id="exist_mail" class="error_notice" style="margin-left:10px; padding:6px; padding-left:45px; display:none;" >Mail gi&agrave; utilizzata</span>
				</li>
				<li>
					<input type="text" name="pass" id="pass" class="text_field small" value="Password" onfocus="document.getElementById('pass').setAttribute('type','password'); document.getElementById('pass').removeAttribute('value');" style="display:inline;" onblur="check_pass()">
					<span id="no_pass" class="error_notice" style="margin-left:10px; padding:6px; padding-left:45px; display:none;" >Inserire la password</span>
				</li>
				<li>
					<input type="text" name="pass2" id="pass2" class="text_field small" value="Ripeti Password" onfocus="document.getElementById('pass2').setAttribute('type','password'); document.getElementById('pass2').removeAttribute('value');" style="display:inline;" onblur="check_pass2()">
					<span id="no_pass2" class="error_notice" style="margin-left:10px; padding:6px; padding-left:45px; display:none;" >Ripetere la password</span>
					<span id="diff_pass" class="error_notice" style="margin-left:10px; padding:6px; padding-left:45px; display:none; z-index:50;" >Le password non coincidono</span>
				</li>
				<li>
					<label for="place" class="auto_clear">Luogo</label>
					<input type="text" name="place" id="place" class="text_field small" >
				</li>
				</ol>
				</form>
				<button onclick="validate()">Modifica</button>
				<?php } 
				else
				{?>
				<h2>Modifica Dati Personali<em class="heading_description"></em></h2>
				<p>Sei collegato tramite un account Facebook quindi non puoi modificare i tuoi dati da RagusaInFoto, ma li puoi modificare solo attraverso il pannello di controllo del tuo account su Facebook stesso.</p>
				<?php
				}
					if($_GET['mod'] == 1)
						echo("<br/><span class=\"success_notice\">Dati aggiornati.</span>");
					if($_GET['mod'] == 2)
						echo("<br/><span class=\"success_notice\">Dati e Password aggiornati.</span>");
					
					
					if(isset($_POST['name']))
					{
						echo("nome = ".$_POST['name']."<br/>");
						echo("cognome = ".$_POST['surname']."<br/>");
						echo("mail = ".$_POST['mail']."<br/>");
						echo("pass = ".$_POST['pass']."<br/>");
						echo("pass2 = ".$_POST['pass2']."<br/>");
						echo("luogo = ".$_POST['place']."<br/>");
						
						//controllo il nome
						if($_POST['name'] == 'Nome')
							$name = $user['name'];
						else
							$name = $_POST['name'];
						//controllo il cognome	
						if($_POST['surname'] == 'Cognome')
							$surname = $user['surname'];
						else
							$surname = $_POST['surname'];
						//controllo il luogo
						if($_POST['place'] == 'Luogo')
							$place = $user['place'];
						else
							$place = $_POST['place'];
						//controllo password
						if($_POST['pass2'] == 'Ripeti Password' || $_POST['pass2'] == '')
						{
							$pass = $user['pass'];
							$new_pass = false;
						}
						else
						{
							$pass = $_POST['pass'];
							$new_pass = true;
						}
						//controllo mail
						if($_POST['mail'] == 'Mail')
						{
							$mail = $user['mail'];
							$send_mail = false;
						}
						else
						{
							$mail = $_POST['mail'];
							$send_mail = true;
						}
						
						update_user_data($name, $surname, $mail, $send_mail, $pass, $place, $_SESSION['id']);
						
						if(!$send_mail)
						{
							//refresh pagina cosi da aggiornare i dati in tabella
							if($new_pass)
								header("Location: profile.php?mod=2");
							else
								header("Location: profile.php?mod=1");
						}
					}
					
?>
			</div>
			<div class="full">
				<h2>Elimina Profilo<em class="heading_description"></em></h2>
				<p><?php echo DELETE_PROFILE; ?></p>
				<button id="delete_continue">Continua</button>
				<br/>
				<p id="delete" style="display: none;"><?php echo DELETE_PROFILE_CONFIRM; ?><br/><br/>
				<a href="delete.php?id=<?php echo $_SESSION['id']; ?>"><button>Si sono sicuro</button></a>
				</p>
				<script>
				$('#delete_continue').click(function() {
				  $('#delete').fadeIn('slow');
				});
				</script>
			</div>
		</div>
	</div>
	<!-- end body content -->
	
	<?php get_footer(); ?>
	
</div>
<!-- end wrapper -->
</body>
</html>